/*
 * Copyright 1999-2015 dangdang.com.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * </p>
 */

package cn.cloudbae.config;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.dangdang.ddframe.job.config.JobCoreConfiguration;
import com.dangdang.ddframe.job.config.simple.SimpleJobConfiguration;
import com.dangdang.ddframe.job.lite.api.JobScheduler;
import com.dangdang.ddframe.job.lite.config.LiteJobConfiguration;
import com.dangdang.ddframe.job.lite.spring.api.SpringJobScheduler;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter;

import cn.cloudbae.schedule.FirstSimpleJob;
import cn.cloudbae.schedule.SecondSimpleJob;
import cn.cloudbae.schedule.ThirdSimpleJob;


@Configuration
public class JobConfig {
	
	//引用zk
    @Autowired
    ZookeeperRegistryCenter zookeeperRegistryCenter;
    
    
    /**
     * http://elasticjob.io/docs/elastic-job-lite/01-start/dev-guide/
     * 
     * Elastic-Job配置分为3个层级，分别是Core、Type、Root。每个层级使用相似于装饰者模式的方式装配。
     * 1. Core对应JobCoreConfiguration，用于提供作业核心配置信息，如：作业名称、分片总数、CRON表达式等。
     * 2. Type对应JobTypeConfiguration，有3个子类分别对应SIMPLE, DATAFLOW和SCRIPT类型作业，提供3种作业需要的不同配置，如：DATAFLOW类型是否流式处理或SCRIPT类型的命令行等。
     * 3. Root对应JobRootConfiguration，有2个子类分别对应Lite和Cloud部署类型，提供不同部署类型所需的配置，如：Lite类型的是否需要覆盖本地配置或Cloud占用CPU或Memory数量等。
     * 4. 如果在Spring工程中使用，则还需要用JobScheduler封装Root。
     */
    
    /**
     * 创建job-bean
     */
    @Bean
    public FirstSimpleJob firstSimpleJob() {
    	return new FirstSimpleJob();
    }
    
    /**
     * 创建job-bean
     */
    @Bean
    public SecondSimpleJob secondSimpleJob() {
    	return new SecondSimpleJob();
    }
    
    /**
     * 创建job-bean
     */
    @Bean
    public ThirdSimpleJob thirdSimpleJob() {
    	return new ThirdSimpleJob();
    }

	/**
	 * job-bean封装为SpringJobScheduler
	 */
    @Bean(initMethod = "init")
    public JobScheduler firstSimpleJobScheduler() {
    	//0. job核心配置参数
    	String jobName = "firstSimpleJobXXX";
    	String cron = "0/15 * * * * ?";  //15秒执行一次
    	int shardingTotalCount = 10; //分片总数
    	//分片参数，一定要从0开始，个数与shardingTotalCount一致
    	//String shardingItemParameters="0=中国,1=韩国,2=日本,3=英国,4=法国,5=西班牙,6=美国,7=巴西,8=阿根廷"; 
    	String shardingItemParameters="0=中国,1=韩国,2=日本,3=英国,4=法国,5=西班牙,6=美国,7=巴西,8=阿根廷,9=南非"; //分片参数
    	
    	//1. core-config
        JobCoreConfiguration jobCoreConfiguration = JobCoreConfiguration.newBuilder(
        		jobName, cron, shardingTotalCount) //job的名称，定时时间，分片个数
        		.shardingItemParameters(shardingItemParameters) //分片参数
        		.description("这是我的firstJob").build();
        
        //2. 把core-config封装为type-config
        SimpleJobConfiguration simpleJobConfiguration = new SimpleJobConfiguration(
        		jobCoreConfiguration, FirstSimpleJob.class.getCanonicalName());
        
        //3. 把type-config封装为root-lite-config
        LiteJobConfiguration liteJobConfiguration = LiteJobConfiguration.newBuilder(
        		simpleJobConfiguration).overwrite(true).build(); //overwrite(true)表示每次启动都覆盖zk注册中心的配置
        
        //4. 把root-lite-config封装为JobScheduler
        return new SpringJobScheduler(firstSimpleJob(), zookeeperRegistryCenter, liteJobConfiguration);
    }
    
    @Bean(initMethod = "init")
    public JobScheduler secondSimpleJobScheduler() {
    	//0. job核心配置参数
    	String jobName = "secondSimpleJobXXX";
    	String cron = "0/25 * * * * ?";  //25秒执行一次
    	int shardingTotalCount = 7; //分片总数
    	String shardingItemParameters="0=比亚迪,1=长安,2=吉利,3=大众,4=宝马,5=奔驰,6=福特"; //分片参数
    	
    	//1. core-config
        JobCoreConfiguration jobCoreConfiguration = JobCoreConfiguration.newBuilder(
        		jobName, cron, shardingTotalCount).shardingItemParameters(shardingItemParameters) 
        		.description("这是我第二个simple job").build();
        
        //2. 把core-config封装为type-config
        SimpleJobConfiguration simpleJobConfiguration = new SimpleJobConfiguration(
        		jobCoreConfiguration, SecondSimpleJob.class.getCanonicalName());

        //3. 把type-config封装为root-lite-config
        LiteJobConfiguration lteJobConfiguration = LiteJobConfiguration.newBuilder(
        		simpleJobConfiguration).overwrite(true).build();
        
        //4. 把root-lite-config封装为JobScheduler
        return new SpringJobScheduler(secondSimpleJob(), zookeeperRegistryCenter, lteJobConfiguration);
    }
    
    @Bean(initMethod = "init")
    public JobScheduler thirdSimpleJobScheduler() {
    	//0. job核心配置参数
    	String jobName = "thirdSimpleJobXXX";
    	String cron = "0/5 * * * * ?";  //5秒执行一次
    	int shardingTotalCount = 1; //分片总数
    	String shardingItemParameters="0=比亚迪"; //分片参数
    	
    	//1. core-config
        JobCoreConfiguration jobCoreConfiguration = JobCoreConfiguration.newBuilder(
        		jobName, cron, shardingTotalCount).shardingItemParameters(shardingItemParameters) 
        		.description("这是我第三个simple job").build();
        
        //2. 把core-config封装为type-config
        SimpleJobConfiguration simpleJobConfiguration = new SimpleJobConfiguration(
        		jobCoreConfiguration, ThirdSimpleJob.class.getCanonicalName());
        
        //3. 把type-config封装为root-lite-config
        LiteJobConfiguration liteJobConfiguration = LiteJobConfiguration.newBuilder(
        		simpleJobConfiguration).overwrite(true).build();
        
        //4. 把root-lite-config封装为JobScheduler
    	return new SpringJobScheduler(thirdSimpleJob(), zookeeperRegistryCenter, liteJobConfiguration);
    }
}
